
class OsnRequest {
    func fromJson(_ type:OsnRequestType, _ json:[String:Any], _ data:[String:Any]) -> DMCCRequest {
        let r = DMCCRequest()
        r.status = OsnRequestStatus.Unread
        if(type == OsnRequestType.Friend){
            r.from = json["from"] as? String ?? ""
        }else if(type == OsnRequestType.ApplyMember){
            r.group = json["from"] as? String ?? ""
            r.from = data["originalUser"] as? String ?? ""
        }
        r.type = type;
        r.direction = OsnRequestDir.Recv;
        r.to = json["to"] as? String ?? ""
        r.reason = data["reason"] as? String ?? ""
        r.timestamp = json["timestamp"] as? Int64 ?? 0
        r.hash = json["hash"] as? String ?? ""
        return r
    }
    func getRequestInfo(_ osnId:String, _ friendId:String) -> DMCCRequest? {
        return dbRequest.getRequestInfo(osnId, friendId)
    }
    func setRequestInfo(_ info:DMCCRequest) -> Bool {
        return dbRequest.addRequestInfo(info)
    }
    func acceptFriend(_ request:DMCCRequest) async ->Bool{
        if await gimService.imRequest("AgreeFriend", request.from, nil) != nil {
            request.setStatus(OsnRequestStatus.Accepted)
            return setRequestInfo(request)
        }
        return false
    }
    func rejectFriend(_ request:DMCCRequest) async ->Bool {
        if await gimService.imRequest("RejectFriend", request.from, nil) != nil {
            request.setStatus(OsnRequestStatus.Rejected)
            return setRequestInfo(request)
        }
        return false
    }
    func acceptMember(_ request:DMCCRequest) async ->Bool {
        let json:[String:Any] = ["userID": request.from]
        if await gimService.imRequest("AgreeMember", request.group, json) != nil {
            request.setStatus(OsnRequestStatus.Accepted)
            return setRequestInfo(request)
        }
        return false

    }
    func rejectMember(_ request:DMCCRequest) async ->Bool {
        let json:[String:Any] = ["userID": request.from]
        if await gimService.imRequest("RejectMember", request.group, json) != nil {
            request.setStatus(OsnRequestStatus.Rejected)
            return setRequestInfo(request)
        }
        return false
    }
    func listRequestReceive()->[DMCCRequest]{
        return dbRequest.listRequestReceive()
    }
    func listRequestSend()->[DMCCRequest]{
        return dbRequest.listRequestSend()
    }
    
}
let gosnRequest = OsnRequest()
